package com.ftg.learn.controller;

import com.ftg.learn.domain.StudentDomain;
import com.ftg.learn.service.StudentService;
import com.ftg.learn.util.ToWeb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author kangjx
 * @date 2024/2/28
 */
@RestController
@Api(tags = "学生维护功能")
/**
 * 当前java类为控制层类,
 * 里面的方法返回值 会直接输出到浏览器上面
 */
public class HelloWorldController {

    @Resource
    StudentService studentService;

    /**
     * getmapping 表示当前这个方法可以使用  get方式访问
     * get 方式表示在浏览器里面直接访问地址  一般就于查询
     * post 表示使用post访问，一般用于添加，或者部分查询
     * delete  删除方法
     * put     修改
     *
     * @RequestParam : url?name=xxx&sex=xxx
     * @RequestBody : json格式  或者是一个java对象  在地址上看不到,是从体里面传递
     * @Pathvariable  : url/{name}/{id}
     *
     *
     * @return
     */
    @GetMapping("/hello")
    @ApiOperation("学生查询功能")
    public ToWeb hello(
           @ApiParam(name = "name",value = "姓名") @RequestParam(value = "name",required = false) String name,
           @ApiParam(name = "sex",value = "性别") @RequestParam(value = "sex",required = false) String sex,
           @ApiParam(name = "start",value = "页数") @RequestParam(value = "start",required = false,defaultValue = "1") Integer start,
           @ApiParam(name = "limit",value = "条数") @RequestParam(value = "limit",required = false,defaultValue = "5") Integer limit
    ){
        return ToWeb.success(studentService.showAll(name,sex,start,limit));
    }

    @PostMapping("/hello1")
    @ApiOperation("学生添加")
    public ToWeb hello1(@RequestBody StudentDomain studentDomain){
        int insert = studentService.insert(studentDomain);
        return insert == -1?ToWeb.failure("添加失败了，学生信息不能为空"): insert == -2?ToWeb.failure("添加失败了，学生学号已存在了"):ToWeb.success(insert);
    }

    @PutMapping("/hello2")
    public String hello2(@RequestParam("name") String name,@RequestBody StudentDomain studentDomain){
        return "hello world---post2";
    }

    @DeleteMapping("/hello3/{id}")
    public String hello3(@PathVariable("id")String id){
        System.out.println(id);
        return "hello world---post";
    }

}
